﻿
var displayRuleUrl = ArrangeUrl + "/HsProgram/getParamList";
var deleteDisplayRuleUrl = ArrangeUrl + "/HsProgram/delete";
var isLinkPlayPaperUrl = HardwareMgrUrl + "/broadcast/getDataByProgramId";


$(function () {
    loadDisplayRuleTable();
});


//加载显示规则列表
function loadDisplayRuleTable() {

    $.ajax({
        url: displayRuleUrl,
        type: 'get',
        data: {
            pageSize: 10,
            pageNumber: 1,
            programName: $("#txt_insname").val()
        },
        success: function (d) {
            if (JSON.parse(d).status == '0') {
                var dataObj = JSON.parse(d).data.list;

                var _html = "";

                for (var i = 0; i < dataObj.length; i++) {
                    var _programId = dataObj[i].programId;
                    var _programName = dataObj[i].programName;
                    var _editTime = dataObj[i].editTime;
                    var _resolution = dataObj[i].resolution;

                    _html += "<tr id='" + JSON.stringify(dataObj[i]) + "'>" +
                            "<td>" + _programName + "</td>" +
                            "<td>" + _editTime + "</td>" +
                            "<td>" + _resolution + "</td>" +
                            "<td  align='right' >" +
                            "      <input type='button'  class='but_edit' value='编辑' onclick='editDRuleInfo(\"" + _programId + "\",\"" + _programName + "\")'/>" +
                            "      <input type='button'  class='but_delete' value='删除' onclick='deleteDRuleInfo(\"" + _programId + "\")'/>" +
                            "</td>" +
                       +"</tr>";
                }
                $("#tbody_ruledisplay").html(_html);
                loadRollingSubTablePagin(JSON.parse(d).data);
            }
        }
    });
}

//修改显示规则
function editDRuleInfo(d, n) {
    setCookie("_RULENAME_" + d, n);
    window.location.href = "../arrange/new_displayrule.html?rule_id=" + d;
}

//删除显示规则
function deleteDRuleInfo(d) {

    layer.confirm('确定要删除该显示规则吗？', {
        btn: ['取消', '确定'],
        icon: 3,
    }, function () {
        layer.closeAll();
    }, function () {
        $.ajax({
            url: isLinkPlayPaperUrl,
            data: {
                programId: d,
            },
            type: "get",
            success: function (v) {
                var dataInfo = JSON.parse(v).data;
                if (dataInfo.length == 0) {
                    $.ajax({
                        url: deleteDisplayRuleUrl,
                        data: {
                            programId: d
                        },
                        type: 'post',
                        success: function (d) {
                            if (JSON.parse(d).status == '0') {
                                loadDisplayRuleTable();
                            } else {
                                layer.alert("删除失败：" + JSON.parse(d).desc, { icon: 2 });
                            }
                        }
                    });
                } else if (dataInfo.length > 0) {
                    layer.alert("删除失败:已有播出单关联此节目，请先解除关联后再删除！", { icon: 2 });
                }
            }
        });
    });
}

function loadRollingSubTablePagin(jData) {

    //异步加载数据
    var _startNumPage = jData.pageNum;
    var _sumNumPage = jData.pages;

    console.log(_startNumPage + "__" + _sumNumPage);
    Page({
        num: _startNumPage,					        //总页码数
        startnum: _startNumPage,				    //指定页码
        elem: $('#ruledisplay_pagebar'),	        //指定的元素
        callback: function (n) {	                //回调函数
            loadDisplayRuleTable(n);
        }
    });

}